﻿using System;
using System.Configuration;

namespace ErrorDumper.Configuration {
    public class ProviderConfigurationElement : ConfigurationElement {

        [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
        public string Name {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }

        [ConfigurationProperty("ref", IsRequired = false, DefaultValue = "")]
        [ConfigurationValidator(typeof(RefValidator))]
        public string Reference {
            get { return (string)this["ref"]; }
            set { this["ref"] = value; }
        }

        [ConfigurationProperty("type", IsRequired = false, DefaultValue = "")]
        public string ProviderType {
            get { return (string)this["type"]; }
            set { this["type"] = value; }        
        }

        class RefValidator : ConfigurationValidatorBase {

            public override bool CanValidate(Type type) {
                return type == typeof(string);
            }

            public override void Validate(object value) {
                var stringValue = value as string;

                if (stringValue != null)
                    switch (stringValue.ToLower()) {
                        case "":
                            return;
                        case "sql":
                            return;
                        case "directory":
                            return;
                    }

                throw new NotSupportedException(string.Format(
                    "Writer reference '{0}' is not supported, it must be 'sql', 'directory' or 'console'",
                    value));
            }
        }
    }
}